package com.nhncorp.pleiades.demo;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

import com.nhncorp.pleiades.client.LockEventData;
import com.nhncorp.pleiades.client.LockEventHandler;
import com.nhncorp.pleiades.client.LockEventMonitor;
import com.nhncorp.pleiades.client.LockService;
import com.nhncorp.pleiades.client.LockServiceClient;
import com.nhncorp.pleiades.client.PleiadesUser;
import com.nhncorp.pleiades.conf.PleiadesConfiguration;
import com.nhncorp.pleiades.protocol.LockMetaData;
import com.nhncorp.pleiades.util.Sleeper;

public class EventClient {

  private PleiadesConfiguration conf;
  private LockService pleiadesClient = null;
  EventHandler eventHandler;
  Thread eventThread;
  AtomicBoolean stopRequested = new AtomicBoolean(false);
  private Sleeper sleeper = null;
  String path1 = "/process/data__" + System.currentTimeMillis();
  String path2 = "/non_process/data_" + System.currentTimeMillis();
  String path3 = "/data_" + System.currentTimeMillis();
  
  public EventClient() {
    conf = new PleiadesConfiguration();
    sleeper = new Sleeper(1000, stopRequested);
    try {
      pleiadesClient = new LockServiceClient(new PleiadesUser("neptune"), conf);
    } catch (IOException e) {
      e.printStackTrace();
    }
    eventHandler = new EventHandler();
    eventThread = new Thread(eventHandler);
    eventThread.start();
  }
  
  private void doEventJob() throws IOException {
    try {
      pleiadesClient.setEventHandler(eventHandler, null);
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("\n\ntry to createNode path1");
    if(!pleiadesClient.createNode(new LockMetaData(path1))) {
      System.out.println("false");
    } 
    sleeper.sleep();

    System.out.println("\n\ntry to createNode path1 with overwrite");
    if(!pleiadesClient.createNode(new LockMetaData(path1), true)) {
      System.out.println("false");
    }
    sleeper.sleep();

    System.out.println("\n\ntry to createNode path2");
    if(!pleiadesClient.createNode(new LockMetaData(path2))) {
      System.out.println("false");
    }
    sleeper.sleep();

    System.out.println("\n\ntry to setContents path2");
    if(!pleiadesClient.setContents(path2, "This is Terry Shim")){
      System.out.println("false");
    }
    sleeper.sleep();

    System.out.println("\n\ntry to setContents path3");
    if(!pleiadesClient.setContents(path3, "This is Terry Shim")) {
      System.out.println("false");
    }
    sleeper.sleep();

    System.out.println("\n\ntry to createNode path3");
    if(!pleiadesClient.createNode(new LockMetaData(path3))) {
      System.out.println("false");
    }
    sleeper.sleep();

    System.out.println("\n\ntry to deleteNode path3");
    if(!pleiadesClient.deleteNode(path3)) {
      System.out.println("false");
    }
    sleeper.sleep();

    System.out.println("\n\ntry to setContents path1");
    if(!pleiadesClient.setContents(path1, "This is Terry Shim")) {
      System.out.println("false");
    }
    sleeper.sleep();

    System.out.println("\n\ntry to deleteNode path1");
    if(!pleiadesClient.deleteNode(path1)) {
      System.out.println("false");
    }
    sleeper.sleep();
    
    pleiadesClient.close();
    stopRequested.set(true);
    
  }
  
  public static void main(String[] args) throws IOException {
    EventClient client = new EventClient();
    client.doEventJob();
  }
  
  public class EventHandler implements Runnable, LockEventHandler {
    public void run() {
      loop();
    }
    private void loop() {
      while(!stopRequested.get()) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
          return;
        }
      }
    }
    public void shutdown() {
      stopRequested.set(true);
    }
    public void nodeAdded(LockEventData eventData) {
      System.out.println("nodeAdded::" + eventData);
    }

    public void nodeModified(LockEventData eventData) {
      System.out.println("nodeModified::" + eventData);
    }

    public void nodeRemoved(LockEventData eventData) {
      System.out.println("nodeRemoved::" + eventData);
    }

    public void contentsModified(LockEventData eventData) {
      System.out.println("contentsModifed::" + eventData);
    }
    public void sessionExpired() {
      System.out.println("handleExpired");
    }
    public void sessionSafe() {
      System.out.println("handleSafe");
    }
    public void sessionWarning() {
      System.out.println("handleWarning");
    }
    public void masterFailover() {
      System.out.println("masterFailover");
    }
    @Override
    public void lockExpired(LockEventData eventData) {
      System.out.println("lockExpired::" + eventData);
    }
    
    @Override
    public void addLockEventMonitor(LockEventMonitor monitor) {
      System.out.println("monitor is added");
    }
    
    @Override
    public void removeEventMonitor(LockEventMonitor monitor) {
      System.out.println("monitor is removed");
    }
    @Override
    public void clientExpired(LockEventData eventData) {
      System.out.println("clientExpired::" + eventData);
      
    }
    @Override
    public void clearLockEventMonitors() {
      // TODO Auto-generated method stub
      
    }
  }
}
